/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.modules.app.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.MapUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.app.entity.*;
import io.renren.modules.app.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * APP登录授权
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/api/")
@Api("看样预约、资讯、贷款服务、办证服务、轮播图")
public class OtherController {

    @Autowired
    private KanyangyuyueService kanyangyuyueService;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private DaikuangshenqingService daikuangshenqingService;

    @Autowired
    private BanzhengService banzhengService;

    @Autowired
    private GuanggaolunbotuService guanggaolunbotuService;

    @Autowired
    private BiaodiwuService biaodiwuService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private FeedbackService feedbackService;
    @Autowired
    private PaimaitixingService paimaitixingService;

    @PostMapping("question/hot")
    @ApiOperation("查询热门问题列表")
    public R questionHotList() {
        QueryWrapper<QuestionEntity> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("hot", 1);
        return R.ok(questionService.list(queryWrapper));
    }

    @PostMapping("question/query")
    @ApiOperation("查询问题列表")
    public R queryQuestionList(@RequestBody PramsDtoQuestion dto) {
        QueryWrapper<QuestionEntity> queryWrapper = new QueryWrapper<>();

        queryWrapper.like("question", dto.getQuestion());
        return R.ok(questionService.list(queryWrapper));
    }

    @Data
    public static class PramsDtoQuestion {
        private String question;
    }

    @PostMapping("question/new")
    @ApiOperation("查询最新问题列表")
    public R questionNewList() {
        QueryWrapper<QuestionEntity> queryWrapper = new QueryWrapper<>();

        queryWrapper.orderByDesc("create_time");

        return R.ok(questionService.list(queryWrapper));
    }

    @PostMapping("kanyangyuyue/save")
    @ApiOperation("添加看样预约")
    public R kanyangyuyueSave(@RequestBody KanyangyuyueEntity kanyangyuyueEntity) {
        String biaodiwuId = kanyangyuyueEntity.getBiaodiwuId();
        if (biaodiwuId != null) {
            BiaodiwuEntity biaodiwuEntity = biaodiwuService.getById(biaodiwuId);
            if (kanyangyuyueEntity != null) {
                kanyangyuyueEntity.setBiaodiwuNumber(biaodiwuEntity.getNumber());
                kanyangyuyueEntity.setBiaodiwuTitle(biaodiwuEntity.getTitle());
            }
        }

        kanyangyuyueEntity.setCreateTime(new Date());
        kanyangyuyueEntity.setStatus(0);
        kanyangyuyueService.save(kanyangyuyueEntity);
        return R.ok();
    }
    @PostMapping("duanxintixing/save")
    @ApiOperation("添加看样预约")
    public R duanxintixingSave(@RequestBody PaimaitixingEntity paimaitixingEntity) {
        String biaodiwuId = paimaitixingEntity.getBiaodiwuId();
        if (biaodiwuId != null) {
            BiaodiwuEntity biaodiwuEntity = biaodiwuService.getById(biaodiwuId);
            if (biaodiwuEntity != null) {
                paimaitixingEntity.setBiaodiwuNumber(biaodiwuEntity.getNumber());
                paimaitixingEntity.setBiaodiwuTitle(biaodiwuEntity.getTitle());
            }
        }

        paimaitixingEntity.setCreateTime(new Date());
        paimaitixingEntity.setDuanxinStatus(0);
        paimaitixingService.save(paimaitixingEntity);
        return R.ok();
    }
    @PostMapping("zixun/list")
    @ApiOperation("查看资讯列表")
    public R zixun(@RequestBody PramsDtoA dto) {
        MapUtils page = new MapUtils().put("page", dto.getPage()).put("limit", dto.getLimit());
        PageUtils list = articleService.queryPage(page, dto.getType(), dto.getKeyword());
        return R.ok(list);
    }

    @PostMapping("zixun/hot")
    @ApiOperation("查看资讯列表")
    public R zixunHot() {
        QueryWrapper<ArticleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hot", 1);
        List<ArticleEntity> list = articleService.list(queryWrapper);
        return R.ok(list);
    }

    @PostMapping("zixun/index")
    @ApiOperation("查看资讯列表")
    public R zixunIndex(@RequestBody PramsDtoA dto) {
        MapUtils page = new MapUtils().put("page", dto.getPage()).put("limit", dto.getLimit());
        PageUtils list = articleService.queryIndexPage(page, dto.getType());
        return R.ok(list);


    }

    @PostMapping("zixun/keywords")
    @ApiOperation("查询资讯关键字列表")
    public R zixunKeywords() {
        QueryWrapper<ArticleEntity> queryWrapper = new QueryWrapper<>();

        queryWrapper.select("distinct keywords");

        List<ArticleEntity> list = articleService.list(queryWrapper);
        List<String> r = new ArrayList<>();
        for (ArticleEntity articleEntity : list) {
            r.add(articleEntity.getKeywords());
        }

        return R.ok(r);
    }


    @GetMapping("zixun/{id}")
    @ApiOperation("查看资讯详情")
    public R zixun(@PathVariable("id") String id) {
        ArticleEntity articleEntity = articleService.getById(id);
        return R.ok(articleEntity);
    }


    @PostMapping("daikuanfuwu/save")
    @ApiOperation("添加贷款服务")
    public R daikuanfuwuSave(@RequestBody DaikuangshenqingEntity daikuangshenqingEntity) {
        daikuangshenqingService.save(daikuangshenqingEntity);
        daikuangshenqingEntity.setCreateTime(new Date());
        return R.ok();
    }

    @PostMapping("banzhengfuwu/save")
    @ApiOperation("添加办证服务")
    public R banzhengfuwuSave(@RequestBody BanzhengEntity banzhengEntity) {
        banzhengEntity.setStatus(0);
        banzhengEntity.setCreateTime(new Date());
        banzhengService.save(banzhengEntity);
        return R.ok();
    }

    @PostMapping("feedback/save")
    @ApiOperation("添加反馈意见")
    public R feedbackSave(@RequestBody FeedbackEntity feedbackEntity) {
        feedbackEntity.setCreateTime(new Date());
        feedbackService.save(feedbackEntity);
        return R.ok();
    }

    @GetMapping("guanggaolunbo/{type}")
    @ApiOperation("返回广告轮播图")
    public R lunbo(@PathVariable("type") Integer type) {
        QueryWrapper<GuanggaolunbotuEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("weizhi_type", type);
        queryWrapper.orderByAsc("sort");
        List<GuanggaolunbotuEntity> list = guanggaolunbotuService.list(queryWrapper);
        return R.ok(list);
    }


    @Data
    public static class PramsDtoA {
        private int page;
        private int limit;
        private int type;
        private String keyword;
    }


}
